Skip to content

gguf : reject empty metadata key names during parsing#22410

Closed
rxho wants to merge 1 commit intoggml-org:masterfrom
rxho:fix/gguf-empty-key-validation
Closed

gguf : reject empty metadata key names during parsing#22410
rxho wants to merge 1 commit intoggml-org:masterfrom
rxho:fix/gguf-empty-key-validation

Conversation

@rxho
Copy link
Copy Markdown

@rxho rxho commented Apr 26, 2026

Summary

Fix #20873 — malformed GGUF files with zero-length key strings trigger GGML_ASSERT(!key.empty()) in the gguf_kv constructor, causing an abort. The parser should reject such files gracefully with an error log instead.

Root Cause

In ggml/src/gguf.cpp, gguf_init_from_file_ptr reads a key string from the file and passes it directly to the gguf_kv constructor without checking for empty keys. The constructor asserts non-empty, which aborts in debug builds and is UB in release.

There is already validation for duplicate keys and bad key sizes, but no check for the zero-length key case.

Fix

Add a guard immediately after the key is read (after the existing bad_alloc catch), before the duplicate-key loop:

if (ok && key.empty()) {
    GGML_LOG_ERROR("%s: empty key name at KV pair %" PRIi64 "\n", __func__, i);
    ok = false;
}

This follows the same pattern as the existing duplicate-key validation.

Changes

File Change
ggml/src/gguf.cpp Reject empty key names with error log
tests/test-gguf.cpp 3 new handcrafted test cases

Test Plan

Three new handcrafted GGUF file types exercise the fix:

Test case Description
KV_EMPTY_KEY All KV pairs have empty keys
KV_EMPTY_KEY_ONLY_FIRST Only the first KV pair has an empty key
KV_EMPTY_KEY_ONLY_MID A middle KV pair has an empty key

All three should fail gracefully (parser returns nullptr / error count incremented) without triggering GGML_ASSERT.

@github-actions github-actions Bot added testing Everything test related ggml changes relating to the ggml tensor library for machine learning labels Apr 26, 2026
@ggml-gh-bot
Copy link
Copy Markdown

ggml-gh-bot Bot commented Apr 26, 2026

Hi @rxho, thanks for your contribution!

Per our contribution guidelines, the automated PR checker found the following issue(s) that need your attention:

  • Multiple open PRs from a new contributor: We limit new contributors (those without a previously merged PR) to 1 open PR at a time. You currently have 2 open PRs.

  • AI-generated content: This project does not accept PRs, descriptions or commit messages that are fully or predominantly AI-generated. If you have used AI to assist you in writing code, please make sure to disclose that explicitly.


Please note that maintainers reserve the right to make final decisions on PRs. If you believe there is a mistake, please comment below.

Fixes ggml-org#20873: a malformed GGUF file with a zero-length key string
triggers GGML_ASSERT(!key.empty()) in the gguf_kv constructor.
Add an early check in gguf_init_from_file_ptr that rejects empty
keys with an error log, consistent with existing duplicate-key
and bad-key-size validation.

Three new handcrafted test cases:
- KV_EMPTY_KEY: all keys are empty
- KV_EMPTY_KEY_ONLY_FIRST: only the first key is empty
- KV_EMPTY_KEY_ONLY_MID: a middle key is empty

Signed-off-by: rxho <ryanxho@outlook.com>
@rxho rxho force-pushed the fix/gguf-empty-key-validation branch from e5bc588 to e934e22 Compare April 27, 2026 00:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ggml changes relating to the ggml tensor library for machine learning testing Everything test related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Misc. bug: malformed GGUF metadata can trigger empty key assert

2 participants